home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * Sounds.c
- *
- ****************************************************************************/
- #include <stdio.h>
- #include <string.h>
-
- #include <Sound.h>
- #include <Speech.h>
-
- #include "Sounds.h"
- #include "StringUtils.h"
-
- // ------------------------------------------------------------------------------------------------
-
- static SpeechChannel fgChannel;
- static short fgInstalledVoice = 0;
- static Boolean fgPrintErrorMessages = false;
- static Boolean fgSpeechMgrPresent = false;
- static Boolean fgSpeechInitialized = false;
-
-
- // ------------------------------------------------------------------------------------------------
-
- static OSErr SayCString (char *string);
- static Boolean GetVoiceByName (char *name, VoiceSpec *voice);
- static Boolean InstallVoice (char *name);
-
- // ---------------------------------------------------------------------
- // This function plays a 'snd ' resource
-
- void
- PlaySound(short sndResourceID)
- {
- OSErr error;
- Boolean kAsync = true;
- SndChannelPtr sndChannel = NULL;
- Handle sndHandle = nil;
-
- // Apple Guidelines for resource id's
- // all negative numbers are reserved for Apple's use
- // 0—127 reserved for Apple use
-
- if (sndResourceID < 1000) {
- sndResourceID = 1000;
- }
-
- sndHandle = GetResource('snd ', sndResourceID);
-
- if (sndHandle == nil)
- {
- SysBeep(2);
- }
- else
- {
- error = SndPlay(sndChannel, (SndListHandle)sndHandle, kAsync);
- ReleaseResource(sndHandle);
- }
- }
-
- // ---------------------------------------------------------------------
- // Speech Manager support
- // ---------------------------------------------------------------------
-
- OSErr
- InitSpeechManager(void)
- {
- long feature = 0L;
-
- OSErr err = Gestalt(gestaltSpeechAttr, &feature);
-
- if (err == noErr) {
- fgSpeechMgrPresent = feature & 1L;
- }
-
- fgSpeechInitialized = true;
-
- return (noErr);
- }
-
- // ---------------------------------------------------------------------
- // This function speaks a c-style string if the Speech Manager is installed
- //
- // Example call:
- // Say("Victoria", "Victoria speaking!"); // Use Victoria voice with MacInTalk Pro synth
- // Say(NULL, "NULL voice speaking!"); // use default synthesizer/voice
- // ---------------------------------------------------------------------
-
- void
- Say(char *voiceName, char *message)
- {
- if (fgSpeechInitialized == false) {
- InitSpeechManager();
- }
-
- if (fgSpeechMgrPresent == false) {
- SysBeep(2);
- return;
- }
-
- if (InstallVoice(voiceName)) {
- SayCString(message);
- }
- }
-
- // ---------------------------------------------------------------------
- // This function always uses the default voice and synthesizer
- // SInce it takes a Pascal string, this can easily be used in
- // Dialog box routines
-
- OSErr
- SayPString(StringPtr string)
- {
- OSErr error = noErr;
-
- if (fgSpeechInitialized == false) {
- InitSpeechManager();
- }
-
- if (fgSpeechMgrPresent)
- {
- if (InstallVoice(NULL))
- {
- while (SpeechBusy())
- ; // wait politely while the other person finishes talking!
- error = SpeakString(string);
- }
- else
- {
- SysBeep(2);
- }
- }
- else
- {
- SysBeep(2);
- }
-
- return error;
- }
-
- // ---------------------------------------------------------------------
- // This function speaks a text handle if the Speech Manager is installed
- // ---------------------------------------------------------------------
-
- OSErr
- SayHandle(char *voiceName, Handle message)
- {
- OSErr error = noErr;
- char savedState;
-
- if (fgSpeechInitialized == false)
- {
- error = InitSpeechManager();
- }
-
- if (error == noErr && fgSpeechMgrPresent)
- {
- if (InstallVoice(voiceName))
- {
- while (SpeechBusy())
- ; // wait politely while the other person finishes talking!
-
- savedState = HGetState(message);
- HLock(message);
- error = SpeakText(fgChannel, (char *)*message, GetHandleSize(message));
- HSetState(message, savedState);
- }
- }
- return error;
- }
-
- // ---------------------------------------------------------------------
-
- static OSErr
- SayCString(char *string)
- {
- OSErr error = noErr;
-
- while (SpeechBusy())
- ; // wait politely while the other person finishes talking!
-
- error = SpeakText(fgChannel, string, strlen(string));
- if (error != noErr)
- SysBeep(2);
-
- return error;
- }
-
- // ---------------------------------------------------------------------
-
- static Boolean
- InstallVoice(char *name)
- {
- OSErr error = noErr;
- VoiceSpec voice;
- Boolean foundIt = false;
- VoiceSpec *voicePtr = &voice;
-
- if (name == NULL || name[0] == '\0') {
- voicePtr = NULL; // use default system voice
- }
- else
- {
- foundIt = GetVoiceByName(name, &voice);
- if (!foundIt)
- {
- voicePtr = NULL;
- }
- }
-
- if (fgChannel != nil)
- {
- DisposeSpeechChannel(fgChannel);
- }
-
- error = NewSpeechChannel(voicePtr, (SpeechChannel*)&fgChannel);
-
- return (error == noErr);
- }
-
- // ---------------------------------------------------------------------
-
- static Boolean
- GetVoiceByName(char *name, VoiceSpec *voice)
- {
- VoiceSpec spec;
- VoiceDescription desc;
- char temp[63+1] = {0};
- short i;
- short numVoices;
- Boolean foundIt = false;
-
- CountVoices(&numVoices);
- for (i=1; i <= numVoices && !foundIt; i++)
- {
- GetIndVoice(i, &spec);
- GetVoiceDescription(&spec, &desc, sizeof(desc));
- BlockMoveData((Ptr)&desc.name, temp, desc.name[0]+1);
- PToCString((StringPtr)temp, temp);
- if (strncmp(name, temp, strlen(name)) == 0)
- {
- foundIt = true;
- }
- }
-
- if (foundIt)
- {
- MakeVoiceSpec(spec.creator, spec.id, voice);
- }
-
- return foundIt;
- }
-
- // ---------------------------------------------------------------------
-